package com.efast.dmom.mgmt.controller.les;

import java.util.List;
import java.util.Map;
import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.dmom.mgmt.entity.les.TtMmManualMri;
import com.efast.dmom.mgmt.entity.master.TmBasPullUnit;
import com.efast.dmom.mgmt.service.les.TtMmManualMriService;
import com.efast.dmom.mgmt.service.master.TmBasPullUnitService;
import com.efast.dmom.mgmt.utils.Assist;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 手工看板需求组织 tt_mm_manual_mri---此表的数据仅针对客户端的手工需求
 * 
 * @author Administrator
 */
@Controller
@RequestMapping(value = "/ttMmManualMri")
@Api(value = "/ttMmManualMri", description = "手工看板需求组织")
public class TtMmManualMriController {

	@Autowired
	private TtMmManualMriService ttMmManualMriService;

	@Autowired
	private TmBasPullUnitService tmBasPullUnitService;

	/**
	 * 通过看板号 查询需要关联的信息
	 */
	@ApiOperation(value = "通过看板号 查询需要关联的信息", httpMethod = "POST", notes = "通过看板号 查询需要关联的信息1")
	@RequestMapping(value = "/queryInfoByPullNo", method = RequestMethod.POST)
	public @ResponseBody ResultBean queryInfoByPullNo(@RequestBody TtMmManualMri ttMmManualMri) {
		ResultBean result = new ResultBean(true, "'");
		List<TmBasPullUnit> listTmBasPullUnit = tmBasPullUnitService
				.selectTmBasPullUnit(new Assist(Assist.andEq("pull_unit_no", ttMmManualMri.getPullUnitNo())));
		if (listTmBasPullUnit.size() == 0) {
			throw new ServiceException("主数据中无法查询到看板卡号" + ttMmManualMri.getPullUnitNo() + "信息");
		}
		TtMmManualMri ttMmManualMri2 = null;
		// 看板模式(上线看板-1，补货看板-2)
		if (listTmBasPullUnit.get(0).getPullType().equals("1")) {
			ttMmManualMri2 = ttMmManualMriService.selectTtMmManualMriByObj(ttMmManualMri);
		}
		if (listTmBasPullUnit.get(0).getPullType().equals("2")) {
			ttMmManualMri2 = ttMmManualMriService.selectReplenishmentTtMmManualMriByObj(ttMmManualMri);
		}
		result.setObject(ttMmManualMri2);
		return result;
	}

	/**
	 * 组单生成上线指示单时，根据工厂、车间、路径分组产生单据，确保同一工厂、车间、路径下的需求在同一单据上， 方便配送人员直接按照配置的路线送货，提高效率
	 * 单据包含头和子信息，产生单据的同时，产生历史记录信息，同时产生对应的出库订单
	 * 请求参数： {"pullUnitNo":[{"pullUnitNo":"KBS01","pullType":"1/2"}]}
	 */
	@SuppressWarnings("unchecked")
	@ApiOperation(value = "确认组单", httpMethod = "POST", notes = "确认组单请求参数===> {\"pullUnitNo\":[{\"pullUnitNo\":\"KBS01\"}]}")
	@RequestMapping(value = "/confirmBill", method = RequestMethod.POST)
	public @ResponseBody ResultBean confirmBill(@RequestBody Map<String, Object> map) {
		ResultBean result = new ResultBean(true, "'");
		List<Map<String, Object>> map1 = (List<Map<String, Object>>) map.get("pullUnitNo");
		// 所有的组单的记录必须的模式都是同一种模式    即要么都是 上线 类型，要么都是补货类型。
		String pullType=(String) map1.get(0).get("pullType");
		for (int i = 1; i < map1.size(); i++) {
			if (!pullType.equals(map1.get(i).get("pullType"))) {
				throw new ServiceException((String)map1.get(i).get("pullUnitNo")+"看板卡号的模式和第一条记录的看板模式不一致");
			}
			if (map1.get(i).get("mriStatus").equals("1")) {
				throw new ServiceException((String)map1.get(i).get("pullUnitNo")+"已组单，不能再次组单");
			}
		}
		ttMmManualMriService.confirmBill(map1);
		result.setMessage("组单成功");
		return result;
	}

	/**
	 * 点击“清空列表”，清空需求数据（页面和数据库数据都清除）
	 */
	@SuppressWarnings("unchecked")
	@ApiOperation(value = "清空列表", httpMethod = "POST", notes = "页面和数据库数据都清除")
	@RequestMapping(value = "/clearList", method = RequestMethod.POST)
	public @ResponseBody ResultBean clearList(@RequestBody Map<String, Object> map) {
		ResultBean result = new ResultBean(true, "'");
		List<Map<String, Object>> ttMmManualMriEmptyBean = (List<Map<String, Object>>) map.get("pullUnitNo");
		for (Map<String, Object> map2 : ttMmManualMriEmptyBean) {
			ttMmManualMriService.deleteTtMmManualMri(
					new Assist(Assist.andEq("tt_mm_manual_mri.pull_unit_no", map2.get("pullUnitNo"))));
		}
		result.setMessage("清空成功");
		return result;
	}

	/**
	 * 选中某行，点击页面“删除”按钮，即可删除选中的那行数据
	 */
	@ApiOperation(value = "删除一行", httpMethod = "POST", notes = "删除一行")
	@RequestMapping(value = "/deleteLine", method = RequestMethod.POST)
	public @ResponseBody ResultBean deleteLine(@RequestBody Map<String, String> map) {
		ResultBean result = new ResultBean(true, "'");
		ttMmManualMriService
				.deleteTtMmManualMri(new Assist(Assist.andEq("tt_mm_manual_mri.pull_unit_no", map.get("pullUnitNo"))));
		result.setMessage("清空成功");
		return result;
	}

	/**
	 * e) 双击某行，可以编辑页面，仅可编辑如下字段，当需求数量更改时，需求箱数自动变更 ，修改完成后点击保存即可
	 */
	@ApiOperation(value = "编辑", httpMethod = "POST", notes = "编辑")
	@RequestMapping(value = "/editLine", method = RequestMethod.POST)
	public @ResponseBody ResultBean editLine(@RequestBody TtMmManualMri ttMmManualMri) {
		ResultBean result = new ResultBean(true, "'");
		ttMmManualMriService.updateNonEmptyTtMmManualMri(ttMmManualMri,
				new Assist(Assist.andEq("tt_mm_manual_mri.pull_unit_no", ttMmManualMri.getPullUnitNo())));
		result.setMessage("编辑成功");
		return result;
	}

	/**
	 * 客户端的手工组织需求每次只能组织一单，且不会记录上次未组织的需求
	 * 具体为：每次重新打开手工上线指示单组织页面时，会清空手工需求tt_mm_manual_mri中的数据
	 */
	@ApiOperation(value = "每次打开页面删除所有的数据", httpMethod = "POST", notes = "每次打开页面删除所有的数据")
	@RequestMapping(value = "/deleteAll", method = RequestMethod.POST)
	public @ResponseBody ResultBean deleteAll() {
		ResultBean result = new ResultBean(true, "'");
		ttMmManualMriService.deleteAllTtMmManualMri();
		result.setObject("删除成功");
		return result;
	}

}
